package com.itany.order.service.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.itany.order.service.IRoderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.table.TableRowSorter;
import java.math.BigDecimal;
import java.util.Date;


@Service
@com.alibaba.dubbo.config.annotation.Service
public class OrderServiceImpl implements IRoderService {

     @Autowired
     private AlipayClient alipayClient;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public String getQrCode() {
        String qrCode = "";
        try {
            //生成唯一单号
            String no = System.currentTimeMillis()+"";
            //声明支付请求对象
            AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
            //声明model对象
            AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
            model.setOutTradeNo(no);//单号
            model.setTotalAmount("0.01");//价格
            model.setSubject("天下画集");//订单主题
            //回调地址
            request.setNotifyUrl("/order/testorder/paySuccess/"+no);
            request.setBizModel(model);
            //执行
            AlipayTradePrecreateResponse response = alipayClient.execute(request);
            //判断响应是否有成功
            if(response.isSuccess()){
                System.out.println("success");
                qrCode=response.getQrCode();
                System.out.println(qrCode);
            }else{
                System.out.println("error");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return qrCode;
    }
}
